package com.whc.common.design.handle.colmun;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.whc.common.Constant;
import com.whc.common.Context;
import com.whc.common.exception.MessageException;
import com.whc.vo.Column;
import com.whc.vo.Table;

import java.time.LocalDate;
import java.time.LocalDateTime;

public class ColumnHandle {
    public static void handler(Table table) {
        for (Column column : table.getColumns()) {
            if (column.getValue() instanceof CharSequence) {
                if (StrUtil.isNotBlank((CharSequence) column.getValue())) {
                    return;
                }
            } else if (column.getValue() != null) {
                return;
            }

            Class<?> clazz = Context.autoColumn.get(column.getKey());
            if (clazz != null) {
                Object value;
                if (Long.class.equals(clazz)) {
                    value = IdWorker.getId();
                } else if (LocalDate.class.equals(clazz)) {
                    value = LocalDate.now();
                } else if (LocalDateTime.class.equals(clazz)) {
                    value = LocalDateTime.now();
                } else {
                    throw MessageException.instance(Constant.Message.TYPE_NOT_FOUND);
                }
                column.setValue(value);
            }
        }
    }
}